package com.jcl.recruit.user.logic;

import com.jcl.core.utils.EncodeMd;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.recruit.user.mapper.UserExternalAccessMapper;
import com.jcl.recruit.user.model.UserExternalAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName UserAccessLogic

 * @Author yyliu
 * @Date 2018/9/14 9:03
 * @Version 1.0
 **/
@Component
public class UserExternalAccessLogic implements UserExternalAccessMapper {

    @Autowired
    private UserExternalAccessMapper userExternalAccessMapper;


    public UserExternalAccess addExternalUserByUsername(String username,
                                                        String password,
                                                        String nickname,
                                                        String category,
                                                        Integer status){
        UserExternalAccess userAccess = new UserExternalAccess();
        userAccess.setNickname(nickname);
        userAccess.setCategory(category);
        userAccess.setStatus(status);
        try {
            userAccess.setPassword(EncodeMd.EncodeByMd5(username));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        userAccess.setPassword(password);
        userAccess.setCreateTime(new Date());
        userAccess.setModifyTime(new Date());
        add(userAccess);
        return userAccess;
    }

    public UserExternalAccess addExternalUserByExternalKey(String externalKey,
                                              String externalUnionKey,
                                              String nickname,
                                              String category,
                                              Integer status){
        UserExternalAccess userAccess = new UserExternalAccess();
        userAccess.setExternalKey(externalKey);
        userAccess.setExternalUnionKey(externalUnionKey);
        userAccess.setNickname(nickname);
        userAccess.setCategory(category);
        userAccess.setStatus(status);
        userAccess.setUsername(externalKey + UserExternalLoginConstant.USER_ACCESS_SPLIT
                + System.currentTimeMillis());
        try {
            userAccess.setPassword(EncodeMd.EncodeByMd5(SystemConfig.getDefaultPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        userAccess.setCreateTime(new Date());
        userAccess.setModifyTime(new Date());
        add(userAccess);

        return userAccess;
    }

    public int changeUseStatusToUnused(String externalKey, String category) {

        return changeStatusesToSetting(UserExternalLoginConstant.USER_ACCESS_STATUS_UNUSED,
                externalKey, category, UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
    }

    public int changeUseStatusToInvalid(String externalKey, String category) {
        return changeStatusesToSetting(UserExternalLoginConstant.USER_ACCESS_STATUS_INVALID,
                 externalKey, category, UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
    }

    public UserExternalAccess findWechatOneByUserId(Integer userId) {
        return userExternalAccessMapper.findOneByUserId(userId, UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);
    }
    @Override
    public List<UserExternalAccess> findList(String category, Integer... status) {
        return userExternalAccessMapper.findList(category, status);
    }

    @Override
    public List<UserExternalAccess> findListByExternalKey(String externalKey,
                                                          String category,
                                                          Integer status) {
        return userExternalAccessMapper.findListByExternalKey(externalKey, category, status);
    }

    @Override
    public List<UserExternalAccess> findListByUsername(String username,
                                                       String category,
                                                       Integer status) {
        return userExternalAccessMapper.findListByUsername(username,category,status);
    }

    @Override
    public UserExternalAccess findOneByExternalKeyAndCode(String externalKey,
                                                          String corpCode) {
        return userExternalAccessMapper.findOneByExternalKeyAndCode(externalKey,corpCode);
    }

    @Override
    public UserExternalAccess findOneByUserId(Integer userId, String category) {
        return userExternalAccessMapper.findOneByUserId(userId, category);
    }

    @Override
    public UserExternalAccess findOne(Integer id) {
        return userExternalAccessMapper.findOne(id);
    }

    @Override
    public int add(UserExternalAccess access) {
        return userExternalAccessMapper.add(access);
    }

    @Override
    public int updateAccess(UserExternalAccess access) {
        return userExternalAccessMapper.updateAccess(access);
    }

    @Override
    public int update(String externalKey,
                      Integer userId,
                      String category) {
        return userExternalAccessMapper.update(externalKey,userId,category);
    }

    @Override
    public int changeStatusesToSetting(Integer setting, String externalKey, String category, Integer... statuses) {
        return userExternalAccessMapper.changeStatusesToSetting(setting, externalKey, category, statuses);
    }

}
